package com.iotechn.unimall.app.api.computer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.domain.ComputerRoomDO;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.ComputerRoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * Generate Code By Unimall
 */
@Service
public class AppRoomServiceImpl implements appRoomService {

    @Autowired
    private ComputerRoomMapper computerRoomMapper;

    @Override
    public boolean delete(Long id,  Long adminId) throws ServiceException {
        return computerRoomMapper.deleteById(id) > 0;
    }

    @Override
    public List<ComputerRoomDO> xiaList(Long adminId) throws ServiceException {
        List<ComputerRoomDO> computerRoomDOS = computerRoomMapper.selectList(new QueryWrapper<ComputerRoomDO>());
        return computerRoomDOS;
    }

    @Override
    public Page<ComputerRoomDO> list(String name, Integer page, Integer limit) throws ServiceException {
        QueryWrapper<ComputerRoomDO> wrapper = new QueryWrapper<ComputerRoomDO>();
        if (name != null) {
            wrapper.like("name", name);
        }
        return computerRoomMapper.selectPage(Page.div(page, limit, ComputerRoomDO.class), wrapper.orderByAsc("id"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ComputerRoomDO create(String name, Long adminId) throws ServiceException {
         Date now = new Date();
         ComputerRoomDO insertDO = new ComputerRoomDO();
         insertDO.setName(name);
         insertDO.setStatus("正常");
         insertDO.setGmtUpdate(now);
         insertDO.setGmtCreate(now);
         if (computerRoomMapper.insert(insertDO) > 0) {
             return insertDO;
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long id, String name, Long adminId) throws ServiceException {
         Date now = new Date();
         ComputerRoomDO updateDO = new ComputerRoomDO();
         updateDO.setId(id);
         updateDO.setName(name);
         updateDO.setGmtUpdate(now);
         if (computerRoomMapper.updateById(updateDO) > 0) {
             return "ok";
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String show(Long id, Long adminId) throws ServiceException {
        List<ComputerRoomDO> computerRoomDOS = computerRoomMapper.selectList(new QueryWrapper<>());
        for (ComputerRoomDO computerRoomDO : computerRoomDOS){
            //id相等默认展示，其他取消默认展示
            if (id.equals(computerRoomDO.getId())){
                computerRoomDO.setShowFlag(1);
            }else {
                computerRoomDO.setShowFlag(0);
            }
            if (computerRoomMapper.updateById(computerRoomDO) > 0) {
                return "ok";
            }
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ng";
    }

}
